package com.micoya.mrplug.nowtime

import net.mamoe.mirai.console.plugins.PluginBase
import net.mamoe.mirai.event.subscribeMessages
import java.awt.Color
import java.util.Calendar
import java.awt.Font
import java.awt.RenderingHints
import java.io.File
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage

object NowTimeMain : PluginBase() {

    override fun onLoad() {
        super.onLoad()
    }

    private val setting by lazy {
        this.loadConfig("now-time/setting.yml")
    }

    private fun getImageWithTime(t: Calendar): BufferedImage {
        val image = ImageIO.read(File(dataFolder.absolutePath + "/" + setting["image_template"]))
        val g = image.createGraphics()

        val x = 410
        var y = 0
        val texts = arrayOf(
            t.get(Calendar.HOUR_OF_DAY).toString(), "点", t.get(Calendar.MINUTE).toString(), "分", "了", "!"
        )

        with(g) {
            font = Font(setting["font"].toString(), Font.BOLD, 45)
            color = Color.BLACK
            setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP)
        }

        for ((i, text) in texts.withIndex()) {
            y += 50
            g.drawString(text, if (i in setOf(0, 2, 5) && text.length == 1) x + 15 else x, y)
        }

        return image
    }

    override fun onEnable() {
        super.onEnable()

        logger.info("Plugin loaded!")

        subscribeMessages {
            (case("现在几点了", trim = true) or case("报时", trim = true)) {
                sendImage(getImageWithTime(Calendar.getInstance()))
            }
            "hello" reply { "你好" }
            contains("哈哈") reply { "嘿嘿" }
            contains("消息长度") reply { it.length }
        }
    }
}
